package com.jml.zlhq.app.ui;

import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.impl.client.DefaultHttpClient;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AutoCompleteTextView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jml.zlhq.app.R;

public class WebHuoDongActivity extends BaseActivity {
	
	private LinearLayout loadpage;
	private TextView loadText;
	private WebView webView;
	private String url;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.hdtxtx_activity);	
		
		LinearLayout leftLay = (LinearLayout) findViewById(R.id.content_header_left_lay);
		//LinearLayout rightLay = (LinearLayout) findViewById(R.id.content_header_right_lay);
		leftLay.setOnClickListener(this);
		//rightLay.setOnClickListener(this);
		AutoCompleteTextView centerEdit = (AutoCompleteTextView) findViewById(R.id.content_header_center_edit);
		centerEdit.setVisibility(View.GONE);
		ImageView leftImage = (ImageView) findViewById(R.id.content_header_left_img);
		leftImage.setImageResource(R.drawable.back_selector);
		ImageView roghtImage1 = (ImageView) findViewById(R.id.content_header_right_img);
		roghtImage1.setVisibility(View.GONE);
		TextView centerText = (TextView) findViewById(R.id.content_header_center_text);
		centerText.setText("活动详情");
		TextView rightText = (TextView) findViewById(R.id.content_header_right_text);
		rightText.setText("  ");
		TextView cityChosed = (TextView) findViewById(R.id.content_header_center_text2);
		cityChosed.setVisibility(View.GONE);
	
		webView = (WebView) findViewById(R.id.txtx_detail_web);
		webView.setVisibility(View.GONE);
		/*********           *********/
		loadpage = (LinearLayout)findViewById(R.id.load_info_lay);
		loadText = (TextView) findViewById(R.id.load_info);		
//		loadpage.setVisibility(View.VISIBLE);
		url = getIntent().getExtras().getString("url");
		System.out.println("url:" + url);
		//url = "http://m.51zlhq.com/member/timer?pid=" + pid + "&";
		/*********           *********/
		initWebView();
		webView.loadUrl(url);				
	}
	 
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.content_header_left_lay:
			finish();
			break;
	
		default:
			break;
		}
	}
	
	
	private void initWebView(){
		webView.setWebChromeClient(new EXWebChromeClient());
		WebSettings webSettings = webView.getSettings();
		webSettings.setJavaScriptEnabled(true);
		webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
		webView.setHorizontalScrollBarEnabled(false);//水平不显示
        webView.setVerticalScrollBarEnabled(false); //垂直不显示
		webView.setWebViewClient(new WebViewClient() {
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				webView.loadUrl(url);
				return true;
			}
		});
		webSettings
				.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
		webSettings.setUseWideViewPort(true);
		webSettings.setLoadWithOverviewMode(true);
		webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
		webView.getSettings().setSavePassword(false);
		
	}
	@Override
	public void loadData(Map<String, String> param) {}
	@Override
	public void clearData() {
		// TODO Auto-generated method stub
		
	}	
	
	/** 
     * 继承WebChromeClient类 
     * 对js弹出框时间进行处理 
     *  
     */  
    private class EXWebChromeClient extends WebChromeClient {  
  
        /** 
         * 处理alert弹出框 
         */  
        @Override  
        public boolean onJsAlert(WebView view,String url, String message, JsResult result) {  
            return super.onJsAlert(view, url, message, result);  
        }  
  
        /** 
         * 处理confirm弹出框 
         */  
        @Override  
        public boolean onJsConfirm(WebView view, String url, String message, JsResult result) {  
//            mReusultText.setText("Confirm:"+message);  
//            result.confirm();  
            return super.onJsConfirm(view, url, message, result);  
        }  
  
        /** 
         * 处理prompt弹出框 
         */  
        @Override  
        public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) {  
//            mReusultText.setText("Prompt input is :"+message);  
//            result.confirm();  
            return super.onJsPrompt(view, url, message, message, result);  
        }  
        
        @Override
        public void onProgressChanged(WebView view,int newProgress){
        	loadText.setText("玩命加载中\t"+newProgress+"%");
        	if(newProgress>=100){
        		//header.getBackground().setAlpha(0);//(activity.getResources().getColor(R.color.full_transparent));
        		loadpage.setVisibility(View.GONE);
        		webView.setVisibility(View.VISIBLE);
        		webView.getSettings().setBlockNetworkImage(false);
        		Log.i("ZLHQ", "页面加载完成");
        	}
        }    
    }  
	
	/**
	 * 自定义的Android代码和JavaScript代码之间的桥梁类
	 * 
	 * @author 1
	 * 
	 */
//	public class WebAppInterface {
//		Context mContext;
//
//		/** Instantiate the interface and set the context */
//		WebAppInterface(Context c) {
//			mContext = c;
//		}
//
//		/** Show a toast from the web page */
//		// 如果target 大于等于API 17，则需要加上如下注解window.thirdLogin.login(param)
//		@JavascriptInterface
//		public void method(String type,String id) {
//			Log.i("====js====", type+","+id);
//		}
//	}
	
	private int getRespStatus(String url) {  
        int status = -1;  
        try {  
                HttpHead head = new HttpHead(url);  
                HttpClient client = new DefaultHttpClient();  
                HttpResponse resp = client.execute(head);  
                status = resp.getStatusLine().getStatusCode();  
        } catch (Exception e) {}  
        return status;  
	} 

}
